﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TerenXNA
{
    class Skybox
    {
        Model SkySphere;
        Effect SkySphereEffect;
        TextureCube SkyboxTexture;

        public Skybox()
        {
            SkySphereEffect = Game1.contentManager.Load<Effect>("SkySphere");
            SkyboxTexture = Game1.contentManager.Load<TextureCube>("Sky4");
            SkySphere = Game1.contentManager.Load<Model>("SphereHighPoly");

            // Set the parameters of the effect
            SkySphereEffect.Parameters["SkyboxTexture"].SetValue(SkyboxTexture);
            // Set the Skysphere Effect to each part of the Skysphere model
            foreach (ModelMesh mesh in SkySphere.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    part.Effect = SkySphereEffect;
                }
            }
        }

        public void draw(Matrix projectionMatrix, Matrix viewMatrix)
        {
            RasterizerState rs = new RasterizerState();
            rs.FillMode = FillMode.Solid;
            Game1.device.RasterizerState = rs;
            // Set the parameters of the effect
            SkySphereEffect.Parameters["SkyboxTexture"].SetValue(SkyboxTexture);
            
            // Set the View and Projection matrix for the effect
            SkySphereEffect.Parameters["ViewMatrix"].SetValue(viewMatrix);
            SkySphereEffect.Parameters["ProjectionMatrix"].SetValue(projectionMatrix);
            // Draw the sphere model that the effect projects onto
            foreach (ModelMesh mesh in SkySphere.Meshes)
            {
                mesh.Draw();
            }
        }

    }
}
